;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var validate = require("validate");
	var css_news_category_list = require("./news_category_list.css");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;

	var init = function() {
		var tpl = $("#newsCategoryTpl").html();
		//		$("#newsListBody").css("display","block");
		initEvent();
		router.init();
		inquireNewsCategoryPageList();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}

		var html_delete = require("../newsCategory/news_category_delete.html");
		var html_detail = require("../newsCategory/news_category_detail.html");
		var html_create = require("../newsCategory/news_category_add.html");

		//点击添加栏目
		$("#main").on("click", "#news-category-add", function() {
			$("#popdialog").html(html_create);
			dialog.showDialog($("#add_category_dialog"), addCategory);
		});


		//点击删除栏目
		$("#newsCategoryBody").on("click", ".table_delete_btn", function() {
			$("#popdialog").html(html_delete);
			var $this = $(this).parents("tr.tr_style");
			var newsCategoryIds = [];
			newsCategoryIds.push($this.attr("newsCategoryId"));
			deletead(newsCategoryIds);
		});

		// 点击编辑栏目
		$("#newsCategoryBody").on("click", ".table_details_link", function() {
			$("#popdialog").html(html_detail);
			var newsCategoryId = $(this).parent().parent().attr("newsCategoryId");
			dialog.showDialog($("#detail_category_dialog"), function(){
				updateNewsCategory(newsCategoryId);
			});
			inquireNewsCategoryDetail(newsCategoryId);
		});

		//搜索按钮
		$(".table-search-button-1").on("click", function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireNewsCategoryPageList();
		});
	};

	//增加栏目
	var addCategory = function() {
		var add_category_dialog = $("#add_category_dialog");
		dialog.showDialog(add_category_dialog, function() {});
		var body = base.parseForm($("#add_category_dialog"));
		body.categoryName = $("#newscategoryName").val();
		body.categoryType = $("#categoryType").val();
		$(".footerBtn").on("click", "#sureUpdFirst", function() {
			base.fnAjax({
				action: "createNewsCategory.json", //接口名称
				async: false, //同步异步
				nocode: false, //是否要登陆，true为不用登陆（其他页面不用传该参数，默认为false）
				oData: body, //传递参数
				fnSuccess: function(data) { //成功回调
					if (data.status == 200) {
						inquireNewsCategoryPageList();
						dialog.hideDialog($("#add_category_dialog"));
					} else {
						base.alert(3, data.message);
					}
					
				}
			});
		});
	};

	//更新栏目
	var updateNewsCategory = function(newsCategoryId) {
		inquireNewsCategoryDetail(newsCategoryId);
		var detail_category_dialog = $("#detail_category_dialog");
		dialog.showDialog(detail_category_dialog, function() {});
		var body = base.parseForm($("#detail_category_dialog"));
		body.newsCategoryId = newsCategoryId;
		body.categoryName = $("#categoryDetailName").val();
		body.categoryType = $("#categoryDetailType").val();
		$(".footerBtn").on("click", "#sureUpdFirst", function() {
			base.fnAjax({
				action: "updateNewsCategory.json",
				oData: body,
				fnSuccess: function(data) {
					if (data.status == 200) {
						inquireNewsCategoryPageList();
						dialog.hideDialog($("#detail_category_dialog"));
					} else {
						base.alert(3, data.message);
					}
					
				}
			});
		});
	};

	//获取资讯栏目信息
	var inquireNewsCategoryDetail = function(newsCategoryId) {
		
		base.fnAjax({
			action: "inquireNewsCategoryDetail.json", //接口名称
			oData: {
				newsCategoryId: newsCategoryId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					$("#categoryDetailName").val(data.datas[0].categoryName);
					$("#categoryDetailType").val(data.datas[0].categoryType);
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	// 点击删除栏目调用接口
	var deletead = function(newsCategoryIds) {
		var delete_category_dialog = $("#delete_category_dialog");
		dialog.showDialog(delete_category_dialog, function() {});
		$(".footerBtn").on("click", "#sure_delete", function() {
			base.fnAjax({
				action: "disableNewsCategory.json", //接口名称
				oData: {
					newsCategoryId: newsCategoryIds
				}, //传递参数
				fnSuccess: function(data) { //成功回调
					if (data.status == 200) {
						inquireNewsCategoryPageList();
					} else {
						base.alert(3, data.message);
					}
					dialog.hideDialog($("#delete_category_dialog"));
				}
			});
		});
	};

	//自动获取资讯栏目列表
	var inquireNewsCategoryPageList = function() {
		var categoryName = $("#categoryTitle").val();
		var updateFromTime =$("#updateFromTime").val();
		var updateToTime =$("#updateToTime").val();
		var categoryType = $("#categoryListType").val();
		base.fnAjax({
			action: "inquireCategoryPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				categoryName: categoryName,
				updateFromTime:updateFromTime,
				updateToTime:updateToTime,
				categoryType:categoryType
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#newscategoryTpl").html();
					var html = juicer(tpl, data);
					$("#newsCategoryBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#newscategorypagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireNewsCategoryPageList();
						});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});